package OfferCode;

/**
 * @Author Kechelle
 * @Date 2020/9/21 21:00
 * @Version 1.0
 */
public class Code15 {
    public static int hammingWeight(int n){
        int count = 0;
        for (int i=0;i<32;i++){
            if (((n>>>i)&1) == 1){
                count++;
            }
        }
        return count;
    }

    public static int hammingWeight2(int n){
        int count = 0;
        while (n!=0){
            count++;
            n&=n-1;
        }
        return count;
    }

    public static int hammingWeight3(int n){
        int count = 0;
        while (n!=0){
           count+=n&1;
           n = n>>>1;
        }
        return count;
    }

    public static void main(String[] args) {
        System.out.println(hammingWeight3(15));
    }
}
